/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.database.db  
 */
package com.apache.database.db;

import java.util.List;

import org.apache.ibatis.session.SqlSessionFactory;

import com.apache.cache.CacheManager;
import com.apache.database.constant.Validator;
import com.apache.database.db.ibatis.DaoSession;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;

/**
 * description:  
 * @author dyhou 创建时间：2016-5-17  
 */
public abstract class DaoSupper implements IDao {

	protected DaoSession ibatisDao;
	protected CacheManager cache;
	private SqlSessionFactory sessionFactory;

	public abstract void init(String nameSpace);

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#insert(com.apache.database.model.MethodParam)  
	 */
	public boolean insert(MethodParam param) {
		int count = ibatisDao.insert(param.getKey(), param.getVaule());
		if (count > 0) {
			if (Validator.isNotNull(param.getCacheId())) {
				cache.createCacheObject(param.getCacheId(), param.getVaule());
			}
			return true;
		}
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#insertBath(com.apache.database.model.MethodParam)  
	 */
	public boolean insertBath(MethodParam param) {
		List list = (List) param.getVaule();
		int count = ibatisDao.insertBath(list);
		if (count > 0)
			return true;
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#edit(com.apache.database.model.MethodParam)  
	 */
	public boolean edit(MethodParam param) {
		int count = 0;
		if (Validator.isEmpty(param.getVaule()))
			count = ibatisDao.update(param.getKey(), param.getParams());
		else
			count = ibatisDao.update(param.getKey(), param.getVaule());
		if (count > 0) {
			if (Validator.isNotNull(param.getCacheId())) {
				cache.removeCacheObject(param.getCacheId());
			}
			return true;
		}
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#editBath(com.apache.database.model.MethodParam)  
	 */
	public boolean editBath(MethodParam param) {
		List list = (List) param.getVaule();
		int count = ibatisDao.updateBath(param.getKey(), list);
		if (count > 0)
			return true;
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#delete(com.apache.database.model.MethodParam)  
	 */
	public boolean delete(MethodParam param) {
		int count = 0;
		if (param.isDelete())
			count = ibatisDao.delete(param.getKey(), param.getParams());
		else
			count = ibatisDao.update(param.getKey(), param.getParams());
		if (count > 0) {
			if (Validator.isNotNull(param.getCacheId())) {
				cache.removeCacheObject(param.getCacheId());
			}
			return true;
		}
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#deleteBath(com.apache.database.model.MethodParam)  
	 */
	@SuppressWarnings("rawtypes")
	public boolean deleteBath(MethodParam param) {
		List list = (List) param.getVaule();
		int count = ibatisDao.deleteBath(param.getKey(), list);
		if (count > 0)
			return true;
		return false;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#count(com.apache.database.model.MethodParam)  
	 */
	public long count(MethodParam param) {
		return ibatisDao.count(param.getKey(), param.getParams());
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#selectSingle(com.apache.database.model.MethodParam)  
	 */
	public Object selectSingle(MethodParam param) {
		Object obj = null;
		if (Validator.isNotNull(param.getCacheId())) {
			obj = cache.getCacheCloneByKey(param.getCacheId());
		}
		if (Validator.isEmpty(obj)) {
			obj = ibatisDao.selectSingle(param.getKey(), param.getParams());
		}
		return obj;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#selectById(com.apache.database.model.MethodParam)  
	 */
	public Object selectById(MethodParam param) {
		Object obj = null;
		if (Validator.isNotNull(param.getCacheId())) {
			obj = cache.getCacheCloneByKey(param.getCacheId());
		}
		if (Validator.isEmpty(obj)) {
			obj = ibatisDao.selectSingle(param.getKey(), param.getInfoId());
			if (!Validator.isEmpty(obj) && Validator.isNotNull(param.getCacheId())) {
				cache.createCacheObject(param.getCacheId(), obj);
			}
		}
		return obj;
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#pageSelect(com.apache.database.model.MethodParam)  
	 */
	public Page pageSelect(MethodParam param) {
		return ibatisDao.selectPage(param.getKey(), param);
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#select(com.apache.database.model.MethodParam)  
	 */
	public List select(MethodParam param) {
		return ibatisDao.select(param.getKey(), param.getParams());
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#getSql(int)  
	 */
	public String getSql(int type) {
		return "";
	}

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.database.db.IDao#initSystemCache()  
	 */
	public void initSystemCache() {

	}

	protected SqlSessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SqlSessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
